<?php

class Documents_model extends CI_Model {

    function  __construct() {
        parent::__construct();
        $this->tableName = "mis_documents";
        $this->load->model("functions_model");
        $this->load->model("consolidate_model");
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function newDocument($added_by, $path) {
        $data = $_POST;
        $data['file_path'] = $path;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function getAll($username) {
        $data['added_by'] = $username;
        $this->db->select("type,descr, file_path, date_added");
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['file_path'] = "<a href=" . base_url() . "uploads/" . $row['file_path'] . ">Download</a>";
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
            $list = $this->consolidate_model->getDocTypes($row['type']);
            if (count($list) > 0) {
                $row['type'] = $list[0]->name;
            }
            $result[] = $row;
        }
        return $result;
    }

    function adminGetAll($type=NULL,$tid=NULL) {
        if ($type != NULL) {
            $data['type'] = $type;
            $this->db->where($data);
        }
        if ($tid != NULL) {
            $data['tid'] = $tid;
            $this->db->where($data);
        }
        $this->db->select("tid,added_by,type,descr, file_path, date_added");
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            $added_by = $row['added_by'];
            $row['added_by'] = "<a href='javascript:loadUrl(\"#doc_result\",\"" . base_url() . "index.php?/admin_mis/applicantProfile/$added_by\")'>" . $row['added_by'] . "</a>";
            $row['file_path'] = "<a href=" . base_url() . "uploads/" . $row['file_path'] . ">Download</a>";
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $list = $this->consolidate_model->getDocTypes($row['type']);
            if (count($list) > 0) {
                $row['type'] = $list[0]->name;
            }
            $row['descr']="<div class='doc'>".$row['descr']."</div>";
            $result[] = $row;
        }
        return $result;
    }

}

?>
